/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduler.fixtures;

import fit.Fixture;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;
import se392.ateam2006.meetingmgmt.MeetingMgmtRemote;
import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import se392.ateam2006.resourcemgmt.ResourceMgmtRemote;
import se392.ateam2006.resourcemgmt.participant.exclusionset.ExclusionRangeEntity;
import se392.ateam2006.user.UserEntity;
import se392.ateam2006.user.UserMgmtRemote;

/**
 *
 * @author Caroline
 */
public class ExclusionSetForParticipantForMeeting extends fit.RowFixture {

    UserMgmtRemote userManage;
    MeetingMgmtRemote meetingManage;
    ResourceMgmtRemote resourceManage;
    
    String email;
    String meetingId;

    public ExclusionSetForParticipantForMeeting(UserMgmtRemote userManage, MeetingMgmtRemote meetingManage, ResourceMgmtRemote resourceManage, String email, String meetingId) {
        this.userManage = userManage;
        this.meetingManage = meetingManage;
        this.resourceManage = resourceManage;
        this.email = email;
        this.meetingId = meetingId;
    }

    @Override
    public Object[] query() throws Exception {
        UserEntity user = userManage.getUserByID(email);
        if (user != null) {
            MeetingEntity meeting = meetingManage.findMeetingById(meetingId);
            if (meeting != null) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd kk:mm");
                ArrayList exclusionList = new ArrayList();
                Collection<ExclusionRangeEntity> exclusions = resourceManage.getExclusionSetsForParticipantAndMeetings(user, meeting);
                for (ExclusionRangeEntity ex : exclusions) {
                    if (ex.isPreferenceSet() == false) {
                        exclusionList.add(new ExclusionRangeEntityShell(
                                format.format(ex.getStartDate().getTime()),
                                format.format(ex.getEndDate().getTime())));
                    }                    
                }
                return exclusionList.toArray();
            }
            System.err.println("Couldn't find meeting " + meetingId);
        }
        System.err.println("Couldn't find user " + email);
        return null;
    }

    @Override
    public Class getTargetClass() {
        return ExclusionRangeEntityShell.class;
    }
}
